package com.doraro.platform.module.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doraro.platform.common.exception.ApiGlobalException;
import com.doraro.platform.module.device.mapper.RepairOrderMapper;
import com.doraro.platform.module.device.model.entity.RepairOrder;
import com.doraro.platform.module.device.model.enums.OrderStatus;
import com.doraro.platform.module.device.model.param.RepairDeviceParam;
import com.doraro.platform.module.device.service.IRepairAdviceService;
import com.doraro.platform.module.device.service.IRepairOrderService;
import com.doraro.platform.module.user.service.ISysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

import static com.doraro.platform.module.device.model.enums.OrderStatus.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author doraro
 * @since 2019-04-21
 */
@Service
public class RepairOrderServiceImpl extends ServiceImpl<RepairOrderMapper, RepairOrder> implements IRepairOrderService {
    @Autowired
    private IRepairAdviceService adviceService;
    @Autowired
    private ISysJobService jobService;

    @Override
    public RepairOrder getUnresolvedOrder(String orderId, String userId) {
        Objects.requireNonNull(orderId);
        Objects.requireNonNull(userId);
        final RepairOrder order = super.lambdaQuery()
                .eq(RepairOrder::getOrderStatus, OrderStatus.APPLY)
                .eq(RepairOrder::getId, orderId)
                .one();
        if (order == null || !order.getCreateUserId().equals(userId)) {
            throw new ApiGlobalException("维修单不存在");
        }
        return order;
    }

    @Override
    public boolean setOrderMaintain(String orderId, String engineerId, String managerId) {
        Objects.requireNonNull(orderId);
        Objects.requireNonNull(engineerId);
        Objects.requireNonNull(managerId);
        return super.lambdaUpdate()
                .eq(RepairOrder::getId, orderId)
                .set(RepairOrder::getOrderStatus, OrderStatus.FIX)
                .set(RepairOrder::getEngineerId, engineerId)
                .set(RepairOrder::getManagerId, managerId)
                .set(RepairOrder::getDispatchTime, LocalDateTime.now())
                .update();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createOrder(RepairDeviceParam param, String userId) {
        final RepairOrder order = param.convert(RepairOrder.class);
        order.setCreateTime(LocalDateTime.now())
                .setOrderStatus(OrderStatus.APPLY)
                .setCreateUserId(userId);
        this.save(order);
        return order.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateByOrderId(String orderId, RepairDeviceParam param, String userId) {
        final RepairOrder order = this.getUnresolvedOrder(orderId, userId);
        final RepairOrder device = param.convert(RepairOrder.class);
        device.setId(order.getId());
        return super.updateById(device);
    }



    @Override
    public RepairOrder completeOrder(String orderId, String engineerId) {
        Objects.requireNonNull(orderId);
        Objects.requireNonNull(engineerId);
        final RepairOrder order = super.getById(orderId);
        if (order == null || !engineerId.equals(order.getEngineerId()) || !order.getOrderStatus().equals(OrderStatus.FIX)) {
            throw new ApiGlobalException("不存在该订单");
        }
        order.setOrderStatus(FIXED_NOT_CONFIRM);
        super.updateById(order);
        return order;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RepairOrder confirmCompleteOrder(String orderId) {
        Objects.requireNonNull(orderId);
        final RepairOrder order = super.getById(orderId);
        if (order == null || !order.getOrderStatus().equals(FIXED_NOT_CONFIRM)) {
            throw new ApiGlobalException("不存在该订单");
        }
        order.setCompleteTime(LocalDateTime.now());
        order.setOrderStatus(FINISH);
        super.updateById(order);
        jobService.accpeptJob(order.getEngineerId(), orderId);
        return order;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rejectOrder(String userId, String orderId) {
        final RepairOrder order = super.getById(orderId);
        if (order == null || !Objects.equals(order.getOrderStatus(), OrderStatus.APPLY)
                || !Objects.equals(order.getCreateUserId(), userId)) {
            throw new ApiGlobalException("不存在该订单");
        }
        adviceService.rejectAdvice(orderId);
        return super.lambdaUpdate()
                .eq(RepairOrder::getId, orderId)
                .set(RepairOrder::getOrderStatus, USER_REJECT)
                .update();
    }


    @Override
    public Integer getCycleTime(String deviceId) {
        return baseMapper.getCycleTime(deviceId);
    }

    @Override
    public IPage<RepairOrder> getUnresolvedOrderByPage(Page<RepairOrder> param, RepairOrder searchParam) {
        final LambdaQueryWrapper<RepairOrder> wrapper = new QueryWrapper<RepairOrder>()
                .lambda()
                .setEntity(searchParam)
                .eq(RepairOrder::getOrderStatus, APPLY);

        return super.page(param, wrapper);
    }


}
